package com.carkeeper.user.service.impl;

import com.carkeeper.user.dao.PermissionDao;
import com.carkeeper.user.pojo.Permission;
import com.carkeeper.user.service.PermissionService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @ClassName PermissionServiceImpl
 * @Description TODO
 * @Author yangbin
 * @Date 2020/11/23 14:28
 * @Version 1.0
 */

@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    /**
     * 新增权限
     * @param permission
     * @return
     */
    @Override
    public int addPermission(Permission permission) {
        return permissionDao.insertSelective(permission);
    }

    /**
     * 删除权限
     * @param id
     * @return
     */
    @Override
    public int deleteById(Integer id) {
        return permissionDao.deleteByPrimaryKey(id);
    }

    /**
     * 修改权限
     * @param permission
     * @return
     */
    @Override
    public int updatePermission(Permission permission) {
        return permissionDao.updateByPrimaryKeySelective(permission);
    }

    /**
     * 根据id查询权限
     * @param id
     * @return
     */
    @Override
    public Permission findById(Integer id) {
        return permissionDao.selectByPrimaryKey(id);
    }

    /**
     * 查询所有权限
     * @return
     */
    @Override
    public List<Permission> findAll() {
        return permissionDao.selectAll();
    }

    /**
     * 分页条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Permission> queryPageByExample(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(searchMap);
        return (Page<Permission>) permissionDao.selectByExample(example);
    }

    /**
     * 根据角色id和权限id为中间表添加数据
     * @param roleId
     * @param permissionId
     * @return
     */
    @Override
    public int addPermissionByRoleId(Integer roleId, Integer permissionId) {
        if (roleId == null) {
            throw new RuntimeException("请选择角色");
        }
        if (permissionId == null) {
            throw new RuntimeException("请选择角色对应的权限");
        }
        return permissionDao.addPermissionByRoleId(roleId, permissionId);
    }

    /**
     * 查询角色对应的权限
     * @param roleId
     * @return
     */
    @Override
    public Set<Permission> findPermissionByRoleId(Integer roleId) {
        return permissionDao.findPermissionByRoleId(roleId);
    }

    /**
     * 根据角色删除对应的权限
     * @param roleId
     * @return
     */
    @Override
    public int deletePermissionByRoleId(Integer roleId) {
        return permissionDao.deletePermissionByRoleId(roleId);
    }

    /**
     * 查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Permission.class);
        Example.Criteria criteria = example.createCriteria();
        // name
        if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
            criteria.andEqualTo("name", searchMap.get("name"));
        }
        // keyword
        if (searchMap.get("keyword") != null && !"".equals(searchMap.get("keyword"))) {
            criteria.andEqualTo("keyword", searchMap.get("keyword"));
        }
        return example;
    }
}
